<?php
class category
{
	public $table;
	public $pages;
	public $db;

	function category()
	{
		global $db;
        $this->db = &$db;
        $this->table = DB_PRE.'category';
	}

    /**
	 * 添加栏目信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add_category( $data )
    {
        $params = $data;
        $res = $this->db->insert( $this->table, $params );
		return $res;
    }

    /**
	 * 获取单条栏目信息
	 *
	 * @param INT $catid
	 * @return ARRAY $category
	 */
    public function get_category( $catid, $siteid=false )
    {
    	$add_sql = '';
    	if($siteid !== false)
    	{
    		$add_sql = ' and siteid=?';
    	}
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and catid = ? {$add_sql} LIMIT 1" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $catid );
        if($siteid !== false)
    	{
    		$this->db->set_string($siteid);
    	}
		$category = $this->db->execute( true );
		return $category;
    }

    /**
	 * 通过路径获取单条栏目信息
	 *
	 * @param INT $url
	 * @return ARRAY $category
	 */
    public function path_get_category( $url )
    {
        $site_id = get_current_site();
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and siteid=? and url = ? LIMIT 1" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $site_id );
        $this->db->set_string( $url );
		$category = $this->db->execute( true );
		return $category;
    }

    /**
	 * 修改栏目信息
	 *
	 * @param ARRAY $data
	 * @param INT $catid
	 * @return BOOL
	 */
    public function edit_category( $data, $catid )
    {
        $params = $data;
		$res = $this->db->simple_update( $this->table, $params, array( 'catid'=>$catid ) );
		return $res;
    }

    /**
	 * 删除栏目信息
	 *
	 * @param INT $catid
	 * @return BOOL
	 */
    public function del_category( $catid )
    {
        return $this->edit_category( array( 'status'=>STATUS_DEL ), $catid );
    }

    /**
	 * 修改栏目新闻信息数
	 *
	 * @param INT $catid, $nums
	 * @return BOOL
	 */
    public function edit_category_nums( $catid, $nums )
    {
        return $this->edit_category( array( 'nums'=> $nums ), $catid );
    }

    /**
	 * 查询所有栏目信息列表信息带分页
	 *
	 * @param ARRAY() $params, STRING $order, INT $page, INT $pagesize
	 * @return 	ARRAY() $list
	 */
    public function get_list( $params, $order, $canview, $page=1, $pagesize=10 )
    {
        $where = '';
        if( is_array( $params ) && sizeof( $params ) > 0 )
        {
            $where = ' where ';
            $where .= implode( ' and ', $params );
        }
        if( is_array( $canview ) )
		{
			if( sizeof( $canview ) <= 0 )
			{
				return array();
			}
			else
			{
				$canview = ' and catid in("' . implode( '","', $canview ) . '")';
			}
		}
		else
		{
			$canview = '';
		}
        $where .= $canview;
        $where .= $order;
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where );
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where . " limit ?, ?" );
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 查询所有栏目信息列表信息无分页
	 *
	 * @param ARRAY() $params, STRING $order
	 * @return 	ARRAY() $list
	 */
    public function get_list_no( $params, $order )
    {
        $where = '';
        if( is_array( $params ) && sizeof( $params ) > 0 )
        {
            $where = ' where ';
            $where .= implode( ' and ', $params );
        }
        $where .= $order;
		$this->db->prepare( "SELECT * FROM $this->table" . $where );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 栏目信息排序
	 *
	 * @param ARRAY() $data
	 * @return 	BOOL
	 */
    public function listorder( $data )
    {
        if( !is_array( $data ) )
        {
            return false;
        }
        foreach( $data as $catid => $listorder )
        {
            $listorder = intval( $listorder );
            $this->edit_category( array( 'listorder' => $listorder ), $catid );
        }
        return true;
    }

    /**
	 * 获得所有栏目ID、名称
	 *
	 * @param 空
	 * @return array $list
	 */
    public function get_all_category($siteid=false)
    {
    	$sql_add = '';
    	if($siteid !== false)
    	{
    		$sql_add = " and siteid='".$siteid."'";
    	}
    	$category = $this->db->query( 'SELECT * FROM ' . $this->table . " where status=".STATUS_NORMAL.$sql_add );
		$category_list = array();
		if( is_array( $category ) )
		{
			foreach ( $category as $c )
			{
				$category_list[$c['catid']] = $c['catname'];
			}
		}
    	return $category_list;
    }

    /**
	 * 返回所有栏目父ID信息（以逗号区分）
	 *
	 * @param INT $catid, STRING $arrparentid, INT $n(目前默认支持5级)
	 * @return 	STRING $arrparentid
	 */
    function get_arrparentid( $catid, $arrparentid = '', $n = 1 )
	{
        global $CATEGORY;
        //如果大于5级，缓存变量$CATEGORY不是数组，缓存变量$CATEGORY没有$catid的栏目
		if( $n > 10 || !is_array( $CATEGORY ) || !isset( $CATEGORY[$catid] ) ) return false;
		$parentid = $CATEGORY[$catid]['parentid'];//获取所传$catid的父ID
		$arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
		if( $parentid )
		{
			$arrparentid = $this->get_arrparentid( $parentid, $arrparentid, ++$n );
		}
		else
		{
			$CATEGORY[$catid]['arrparentid'] = $arrparentid;
		}
		return $arrparentid;
	}

    /**
	 * 返回栏目父路径
	 *
	 * @param INT $catid
	 * @return 	STRING
	 */
    function get_parentdir( $catid )
	{
        global $CATEGORY;
		if( $CATEGORY[$catid]['parentid'] == '0' ) return '';
		$arrparentid = $CATEGORY[$catid]['arrparentid'];
		$arrparentid = explode( ',', $arrparentid );
		$arrcatdir = array();
        
		foreach( $arrparentid as $id )
		{
			if( $id=='0' || empty ($id) ) continue;
			$arrcatdir[] = $CATEGORY[$id]['catdir'];
		}
		return sizeof($arrcatdir)==0 ? '' : implode( '/', $arrcatdir ) . '/';
	}

    /**
	 * 检查同一父路径下是否有重复的栏目目录
	 *
	 * @param INT $parentid，STRING $catdir
	 * @return 	ARRAY()  $category
	 */
    public function check_dir( $parentid, $catdir, $siteid )
    {
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and parentid = ? and catdir = ? and siteid=?" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $parentid );
        $this->db->set_string( $catdir );
        $this->db->set_string( $siteid );
		$category = $this->db->execute();
		return $category;
    }

    /**
	 * 检查同一父路径下是否有重复的栏目名称
	 *
	 * @param INT $parentid，STRING $catname
	 * @return 	ARRAY()  $category
	 */
    public function check_catname( $parentid, $catname, $siteid )
    {
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and parentid = ? and catname = ? and siteid=?" );
        $this->db->set_int(STATUS_NORMAL);
        $this->db->set_string( $parentid );
        $this->db->set_string( $catname );
        $this->db->set_string( $siteid );
		$category = $this->db->execute();
		return $category;
    }
}
?>